#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

#include "StackOSI.h"

using boost::asio::ip::tcp;

int main(int argc, char* argv[])
{
	try
	{
		if (argc != 2)
		{
			std::cerr << "Usage: client <host>" << std::endl;
			return 1;
		}

		boost::asio::io_service io_service;

		tcp::resolver resolver(io_service);
		tcp::resolver::query query(argv[1], "daytime");
		tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

		tcp::socket socket(io_service);
		boost::asio::connect(socket, endpoint_iterator);

		for (;;)
		{
			boost::array<char, 4096> buf;
			boost::system::error_code error;

			size_t len = socket.read_some(boost::asio::buffer(buf), error);

			if (error == boost::asio::error::eof)
				break; // Connection closed cleanly by peer.
			else if (error)
				throw boost::system::system_error(error); // Some other error.

			std::stringstream str(buf.data());
			boost::archive::text_iarchive iar(str);

			IStream* stream = new CByteStream;
			CPacket packet(stream);

			iar >> packet;

			CStackOSI::getInstance().init();
			CStackOSI::getInstance().Decode(packet);

			std::cout << packet.getBufferContent() << std::endl; 
		}
	}
	catch (std::exception& e)
	{
		std::cerr << e.what() << std::endl;
	}

	return 0;
}